package dao;

import model.Book;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

public class BookAdminDao {
    String book_table = "book";
    Database database = new Database();
    ArrayList<Book> arrayList = new ArrayList<>();
    public static final Logger logger = LogManager.getLogger(BookAdminDao.class);

    /**
     * 增加图书
     *
     * @param book book对象
     * @return 是否添加成功
     */
    public boolean addBook(Book book) {
        if (book.getName() == null) {
            return false;
        } else {
            try {
                String addBook = "insert into " + book_table + " (typeId,name,price,`desc`,pic,publish,author,stock,address) values(?,?,?,?,?,?,?,?,?)";
                return preBook(book, addBook);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 删除图书
     *
     * @param id bookId
     * @return boolean 是否成功删除
     */
    public boolean delBook(int id) {
        try {
            String delBook = "delete from " + book_table + " where id = " + id;
            logger.info("delBook SQL:" + delBook);
            try (PreparedStatement preparedStatement = database.getPreparedStatement(delBook)) {
                int isSuccess = preparedStatement.executeUpdate();
                logger.info("delBook SQL query:" + isSuccess);
                return isSuccess != 0;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改图书
     *
     * @param book 新的book对象
     * @return 是否成功修改
     */
    public boolean modBook(Book book) throws SQLException {
        int bookId = book.getId();
        logger.info("modBook by id = " + bookId);
        String modBook = "update " + book_table + " set typeId = ?, name = ?,price = ?, `desc` = ?, pic = ?, publish = ?, author = ?, stock = ?, address = ? where id =" + bookId;
        try {
            return preBook(book, modBook);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private boolean preBook(Book book, String bookSQL) throws SQLException {
        PreparedStatement preparedStatement = database.getPreparedStatement(bookSQL);
        preparedStatement.setInt(1, book.getTypeId());
        preparedStatement.setString(2, book.getName());
        preparedStatement.setDouble(3, book.getPrice());
        preparedStatement.setString(4, book.getDesc());
        preparedStatement.setString(5, book.getPic());
        preparedStatement.setString(6, book.getPublish());
        preparedStatement.setString(7, book.getAuthor());
        preparedStatement.setInt(8, book.getStock());
        preparedStatement.setString(9, book.getAddress());
        logger.info("preBook SQL:" + preparedStatement);
        preparedStatement.executeUpdate();
        logger.info("preBook name:" + book.getName() + "success!");
        return true;
    }

}
